package com.bjpowernode.crm.settings.web.controller;

import com.bjpowernode.crm.commons.utils.Result;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.DicType;
import com.bjpowernode.crm.settings.domain.DicValue;
import com.bjpowernode.crm.settings.service.DicTypeService;
import com.bjpowernode.crm.settings.service.DicValueService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * ClassName: DictypeValueController<br/>
 * Package: com.bjpowernode.crm.settings.web.controller<br/>
 * Description: <br/>
 * Date: 2022/7/4 1:29<br/>
 * Auther:对的我写的，错的我同事写的<br/>
 */
@Controller
public class DictypeValueController {

    @Resource
    private DicValueService dicValueService;
    @Resource
    private DicTypeService dicTypeService;

    @RequestMapping("/settings/dictionary/dicValue/index.do")
    @ResponseBody
    public Object getDicValue()
    {
        List<DicValue> dicValues = dicValueService.getDicValues();
        return dicValues;
    }
    @RequestMapping("/settings/dictionary/DicValue/toDicValuePage.do")
    public String toDicValuePage()
    {
        return "settings/dictionary/value/save";
    }

    @RequestMapping("/settings/dictionary/DicValue/getDicTypes.do")
     @ResponseBody
    public Object getDicTypes()
    {
        List<DicType> dicTypes = dicTypeService.queryAllDictypes();
        return dicTypes;
    }

    @RequestMapping("/settings/dictionary/DicValue/addDicValues.do")
    @ResponseBody
    public Object addDicValues(DicValue dicValue)
    {
        dicValue.setId(UUIDUtils.getUUID());
        try {
            Integer count = dicValueService.insertDicVal(dicValue);
            if(count != 1)
            return Result.fail("添加失败！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("添加异常!");
        }
        return Result.success();
    }

    @RequestMapping("/settings/dictionary/Dicvalue/toEditPage.do")
    public String toEditPage(HttpServletRequest request, @RequestParam(value = "id")String id)
    {
       String[] newId = id.split("-");
       id=newId[0];
       DicValue dicValue =  dicValueService.getDicValueById(id);

           request.setAttribute("dicValue",dicValue);
           return "settings/dictionary/value/edit";
    }

    @RequestMapping("/settings/dictionary/dicValue/updateEdit.do")
    @ResponseBody
    public Object updateEdit(DicValue dicValue)
    {
        try {
            Integer count = dicValueService.updateDicVal(dicValue);
            if(count != 1)
           return Result.fail("更新失败！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("更新异常！");
        }
        return Result.success();
    }

    @RequestMapping("/settings/dictionary/dicValue/delValuesByIds.do")
    @ResponseBody
    public Object delValuesByIds(@RequestParam(value = "id") String[] ids)
    {
        Integer count =0;
        try {

            count = dicValueService.delDicValsByIds(ids);
            if(count !=ids.length)
            {
                return Result.fail("删除失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("删除异常！");
        }
        return Result.success("您删除了："+count+"条数据");
    }

}
